<template>
  <div class="home">
    <nav-bar class="home-nav">
      <template v-slot:center>购物街</template>
    </nav-bar>
    <tab-control
      :title="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      :class="{ fix: tabappear }"
    />
    <bscroll
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      @pullingUp="loadMore"
    >
      <swiper class="homeswiper">
        <swiper-item v-for="item in banner" :key="item">
          <a :href="item.link">
            <img :src="item.image" alt="" @load="swiperLoad" />
          </a>
        </swiper-item>
      </swiper>
      <recommend :recommend="recommend" />
      <feature />
      <tab-control
        :title="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      />
      <good-list :goods="showGoods" />
    </bscroll>
    <back-top @click.native="backtopClick" v-show="isShow"></back-top>
  </div>
</template>
<script>
import { getHomeRequest, getHomeGoods } from "network/home";

import Bscroll from "components/commen/bscroll/Bscroll";
// import BackTop from "components/commen/backTop/BackTop";

import { backTop, bscroll } from "commen/mixin";

import NavBar from "components/commen/navbar/NavBar";
import { Swiper, SwiperItem } from "components/commen/swiper";
import Recommend from "./homechild/Recommend";
import Feature from "./homechild/Feature.vue";
import GoodList from "components/content/goods/GoodList";

import TabControl from "components/content/tabControl/TabControl";
export default {
  name: "Home",
  components: {
    Bscroll,
    // BackTop,
    NavBar,
    Swiper,
    SwiperItem,
    Recommend,
    Feature,
    TabControl,
    GoodList,
  },
  mixins: [backTop, bscroll],
  data() {
    return {
      banner: [],
      recommend: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentIndex: "pop",
      // tabOffestTop: 0,
      isLoad: false,
      // tabappear: false,
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.currentIndex].list;
    },
  },
  created() {
    //1  请求多个数据
    this.getHomeRequest();
    //2  请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  // unmounted() {
  //   console.log(11111111111);
  // },
  mounted() {},
  methods: {
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentIndex = "pop";
          break;
        case 1:
          this.currentIndex = "new";
          break;
        case 2:
          this.currentIndex = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    loadMore() {
      this.getHomeGoods(this.currentIndex);
    },
    swiperLoad() {
      //  tab-control 的吸顶效果
      if (!this.isLoad) {
        this.tabOffestTop = this.$refs.tabControl2.$el.offsetTop + 40;
        this.isLoad = true;
      }
    },
    backtopClick() {
      this.$refs.scroll.scrollTo(0, 0);
    },

    //   网络请求
    getHomeRequest() {
      getHomeRequest().then((res) => {
        this.banner = res.data.banner.list;
        this.recommend = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        this.$refs.scroll.finishPullUp();
      });
    },
  },
};
</script>
<style scoped>
.home-nav {
  background-color: #ff919c;
  position: fixed;
  color: #fff;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9;
}

.fix {
  position: fixed;
  top: 44px;
  left: 0;
  right: 0;
  z-index: 99;
}
</style>